package com.wy.controller;

import com.wy.pojo.Code;
import com.wy.pojo.Token;
import com.wy.pojo.User;
import com.wy.service.CodeService;
import com.wy.service.TokenService;
import com.wy.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * description UserController
 * author wy
 * date 2021-09-01 21:46
 **/
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController
{
    @Autowired
    private UserService userService;

    @Autowired
    private CodeService codeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 注册
     */
    @GetMapping("/register/{email}/{password}/{code}")
    private Integer register(@PathVariable String email,
                          @PathVariable String password,
                          @PathVariable String code) {
        Code dbCode = codeService.getCodeByEmail(email);
        if (dbCode.getExpiretime().before(new Date())) {
            //"验证码已过期,请重新发送"
            return 2;
        }
        if (!dbCode.getCode().equalsIgnoreCase(code)) {
            //"验证码有误"
            return 1;
        }
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        userService.addUser(user);
        return 0;
    }

    /**
     * 校验邮箱是否注册
     * @param email
     */
    @GetMapping("/isRegistered/{email}")
    private boolean isRegistered(@PathVariable String email) {
        User userByEmail = userService.getUserByEmail(email);
        if (userByEmail != null) {
            return true;
        }
        return false;
    }

    /**
     * 登录
     */
    @GetMapping("/login/{email}/{password}")
    private String login(@PathVariable String email,
                       @PathVariable String password, HttpServletResponse response) throws IOException
    {
        User userByEmail = userService.getUserByEmail(email);
        if (userByEmail != null && userByEmail.getPassword().equals(password)) {
            Token token = new Token();
            String tid = UUID.randomUUID().toString();
            token.setToken(tid);
            token.setEmail(email);
            tokenService.addToken(token);
            addCookie(response, "token", token.getToken());
            return email;
        }
        return "false";
    }

    /**
     * 获取邮箱
     * @param token
     */
    @GetMapping("/info/{token}")
    private String getEmail(@PathVariable String token) {
        return tokenService.getEmailByToken(token);
    }

    public void addCookie(HttpServletResponse response,String key, String value){
        Cookie cookie = new Cookie(key, value);//创建新cookie
        cookie.setMaxAge(5 * 60);// 设置存在时间为5分钟
        cookie.setPath("/");//设置作用域
        response.addCookie(cookie);//将cookie添加到response的cookie数组中返回给客户端
    }
}
